﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace VideoSDK
{
    public enum StreamType : int
    {
        Master = 0 >> 31,
        Slave = 1 >> 31
    };

    public enum LinkMode : int
    {
        TCP = 0,
        UDP = 1,
        MultiCast = 2,
        RTP = 3,
        VA = 4
    };

    public struct NET_DVR_DEVICEINFO_V30
    {
        /// <summary>
        /// 序列号
        ///     public byte sSerialNumber[SERIALNO_LEN];
        /// </summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = HCNetSDK.SERIALNO_LEN)]
        public byte[] sSerialNumber;
        /// <summary>
        /// 报警输入个数
        /// </summary>
        public byte byAlarmInPortNum;
        /// <summary>
        /// 报警输出个数
        /// </summary>
        public byte byAlarmOutPortNum;
        /// <summary>
        /// 硬盘个数
        /// </summary>
        public byte byDiskNum;
        /// <summary>
        /// 设备类型, 1:DVR 2:ATM DVR 3:DVS ......
        /// </summary>
        public byte byDVRType;
        /// <summary>
        /// 模拟通道个数
        /// </summary>
        public byte byChanNum;
        /// <summary>
        /// 起始通道号,例如DVS-1,DVR - 1
        /// </summary>
        public byte byStartChan;
        /// <summary>
        /// 语音通道数
        /// </summary>
        public byte byAudioChanNum;
        /// <summary>
        /// 最大数字通道个数
        /// </summary>
        public byte byIPChanNum;
        /// <summary>
        /// 保留
        ///     public byte byRes1[24];
        /// </summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 24)]
        public byte[] byRes1;
    }

    public struct NET_DVR_CLIENTINFO
    {
        /// <summary>
        /// 通道号
        /// </summary>
        public int lChannel;
        /// <summary>
        /// 最高位(31)为0表示主码流，为1表示子，0－30位表示码流连接方式: 0：TCP方式,1：UDP方式,2：多播方式,3 - RTP方式，4-音视频分开(TCP)
        /// </summary>
        public int lLinkMode;
        /// <summary>
        /// 播放窗口的句柄,为NULL表示不播放图象
        ///     IntPtr hPlayWnd;
        /// </summary>
        public IntPtr hPlayWnd;
        /// <summary>
        /// 多播组地址
        ///     char* sMultiCastIP;
        /// </summary>
        public string sMultiCastIP;
    }

    public static class HCNetSDK
    {
        /// <summary>
        /// 序列号长度
        /// </summary>
        public const int SERIALNO_LEN = 48;

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_Init();

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_Cleanup();


        [DllImport("HCNetSDK.dll")]
        public static extern int NET_DVR_Login_V30(string ip, ushort port, string user, string password, out NET_DVR_DEVICEINFO_V30 deviceInfo);

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_Logout_V30(int lUserID);


        [DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory")]
        internal extern static long CopyMemory(IntPtr dest, IntPtr source, uint size);

        public delegate void RealDataCallBack_V30(int lRealHandle, uint dwDataType, IntPtr pBuffer, uint dwBufSize, IntPtr pUser);

        [DllImport("HCNetSDK.dll")]
        public static extern int NET_DVR_RealPlay_V30(int lUserID, ref NET_DVR_CLIENTINFO lpClientInfo, RealDataCallBack_V30 fRealDataCallBack_V30, IntPtr pUser, bool bBlocked);

        [DllImport("HCNetSDK.dll")]
        public static extern int NET_DVR_RealPlay(int lUserID, ref NET_DVR_CLIENTINFO lpClientInfo);

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_StopRealPlay(int lRealHandle);


        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_SaveRealData(int lRealHandle, string sFileName);

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_StopSaveRealData(int lRealHandle);

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_CapturePicture(int lRealHandle, string sPicFileName);


        #region 云台控制命令
        /// <summary>
        /// 接通灯光电源
        /// </summary>
        public const uint LIGHT_PWRON = 2;
        /// <summary>
        /// 接通雨刷开关
        /// </summary>
        public const uint WIPER_PWRON = 3;
        /// <summary>
        /// 接通风扇开关
        /// </summary>
        public const uint FAN_PWRON = 4;
        /// <summary>
        /// 接通加热器开关
        /// </summary>
        public const uint HEATER_PWRON = 5;
        /// <summary>
        /// 接通辅助设备开关
        /// </summary>
        public const uint AUX_PWRON1 = 6;
        /// <summary>
        /// 接通辅助设备开关
        /// </summary>
        public const uint AUX_PWRON2 = 7;
        /// <summary>
        /// 设置预置点
        /// </summary>
        public const uint SET_PRESET = 8;
        /// <summary>
        /// 清除预置点
        /// </summary>
        public const uint CLE_PRESET = 9;
        /// <summary>
        /// 快球转到预置点
        /// </summary>
        public const uint GOTO_PRESET = 39;
        /// <summary>
        /// 焦距以速度SS变大(倍率变大) 
        /// </summary>
        public const uint ZOOM_IN = 11;
        /// <summary>
        /// 焦距以速度SS变小(倍率变小)
        /// </summary>
        public const uint ZOOM_OUT = 12;
        /// <summary>
        /// 焦点以速度SS前调 
        /// </summary>
        public const uint FOCUS_NEAR = 13;
        /// <summary>
        /// 焦点以速度SS后调
        /// </summary>
        public const uint FOCUS_FAR = 14;
        /// <summary>
        /// 光圈以速度SS扩大
        /// </summary>
        public const uint IRIS_OPEN = 15;
        /// <summary>
        /// 光圈以速度SS缩小
        /// </summary>
        public const uint IRIS_CLOSE = 16;
        /// <summary>
        /// 云台以SS的速度上仰
        /// </summary>
        public const uint TILT_UP = 21;
        /// <summary>
        /// 云台以SS的速度下俯
        /// </summary>
        public const uint TILT_DOWN = 22;
        /// <summary>
        /// 云台以SS的速度左转
        /// </summary>
        public const uint PAN_LEFT = 23;
        /// <summary>
        /// 云台以SS的速度右转
        /// </summary>
        public const uint PAN_RIGHT = 24;
        /// <summary>
        /// 云台以SS的速度上仰和左转
        /// </summary>
        public const uint UP_LEFT = 25;
        /// <summary>
        /// 云台以SS的速度上仰和右转
        /// </summary>
        public const uint UP_RIGHT = 26;
        /// <summary>
        /// 云台以SS的速度下俯和左转
        /// </summary>
        public const uint DOWN_LEFT = 27;
        /// <summary>
        /// 云台以SS的速度下俯和右转
        /// </summary>
        public const uint DOWN_RIGHT = 28;
        /// <summary>
        /// 云台以SS的速度左右自动扫描
        /// </summary>
        public const uint PAN_AUTO = 29;
        #endregion

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_PTZControl(int lRealHandle, uint dwPTZCommand, uint dwStop);

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_PTZControlWithSpeed(int lRealHandle, uint dwPTZCommand, uint dwStop, uint dwSpeed);


        public delegate void SerialDataCallBack(int lSerialHandle, IntPtr pRecvDataBuffer, uint dwBufSize, uint dwUser);

        [DllImport("HCNetSDK.dll")]
        public static extern int NET_DVR_SerialStart(int lUserID, int lSerialPort, SerialDataCallBack fSerialDataCallBack, uint dwUser);

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_SerialSend(int lSerialHandle, int lChannel, IntPtr pSendBuf, uint dwBufSize);

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_SerialStop(int lSerialHandle);

        public delegate void DrawFun(int lRealHandle, IntPtr hDc, uint dwUser);

        [DllImport("HCNetSDK.dll")]
        public static extern bool NET_DVR_RigisterDrawFun(int lRealHandle, DrawFun fDrawFun, uint dwUser);

    }

    

    
}
